package com.hspedu.inputstream_;

import com.hspedu.outputstream.Dog;


import java.io.*;

/**
 * @author DL5O
 * @version 1.0
 */
public class ObjectInputStream_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //指定反序列化的文件
        String filePath = "D:\\data.dat";

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));

        //1.读取(反序列化)的顺序一定要和保存数据(序列化)的顺序一致
        //2.否则会出现异常
        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        Object dog = ois.readObject(); //底层Object -> Dog 编译类型是Object，运行类型是Dog
        System.out.println("运行类型=" + dog.getClass());
        System.out.println("dog信息=" + dog);

        //这里有个特别重要的细节
        //1.如果我们希望调用dog方法，就需要向下转型
        //2.需要我们将Dog类拷贝到引用的地方
        Dog dog2 =(Dog) dog;

        //关闭外层流，底层会自动关闭传入的字节流
        ois.close();
    }
}


